﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using octopus_project.enumeration;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using octopus_project.scene.cannon.weapon;

namespace octopus_project.tileset.mapCells {
    class MediumCannon_MapCell : MapCell {
        
        private TileType typeOfMediumCannon;
        private MediumCannon cannon;

        private MediumCannon_MapCell(TileType typeMediumCannon, StrongCell strong, int xParam, int yParam, Player player, MediumCannon mediumCannon) {
            tileType = TileType.MEDIUM_CANNON;
            typeOfMediumCannon = typeMediumCannon;

            base.StrongCell = strong;
            x = (byte)xParam;
            y = (byte)yParam;
            cannon = mediumCannon;
        }

        public override void paint(SpriteBatch spriteBatch, int x, int y, TileMap_GameplayEntry map) {
            base.paint(spriteBatch, x, y, map);

            PlayersColor playerColor = PlayersColor.UNDEFINED;
            if (player != null) {
                playerColor = player.Color;
            }

             spriteBatch.Draw(
                TileTypeExtensions.SpriteImage,
                new Rectangle(x * Constant.TILE_SIDE, y * Constant.TILE_SIDE, Constant.TILE_SIDE, Constant.TILE_SIDE),
                TileTypeExtensions.GetSourceRectangle(typeOfMediumCannon, playerColor),
                Color.White);

        }

        public override bool isWeapon() {
            return true;
        }

        public override MapCell change(TileType tileTypeParam) {
            tileType = TileType.MEDIUM_CANNON;
            return this;

        }

        public static bool createCannon(byte x, byte y, Player playerParam, MediumCannon mediumCannon) { 
            MapCell mapL_U = Game1.battlegroundMap.getMapCell(x,y);
            MapCell mapR_U = Game1.battlegroundMap.getMapCell(x+1,y);
            MapCell mapL_D = Game1.battlegroundMap.getMapCell(x,y+1);
            MapCell mapR_D = Game1.battlegroundMap.getMapCell(x+1,y+1);

            

            if (mapL_U != null && mapR_U != null && mapL_D != null && mapR_D != null
                && mapL_U.tileType == TileType.PLAYER_CONQUERED && mapR_U.tileType == TileType.PLAYER_CONQUERED
                && mapL_D.tileType == TileType.PLAYER_CONQUERED && mapR_D.tileType == TileType.PLAYER_CONQUERED) {

                    StrongCell strong = new StrongCell(3);


                    Game1.battlegroundMap.setMapCell(x, y, new MediumCannon_MapCell(TileType.MEDIUM_CANNON, strong, x, y, playerParam, mediumCannon));
                    Game1.battlegroundMap.setMapCell(x + 1, y, new MediumCannon_MapCell(TileType.MEDIUM_CANNON_R_UP, strong, x + 1, y, playerParam, mediumCannon));
                    Game1.battlegroundMap.setMapCell(x, y + 1, new MediumCannon_MapCell(TileType.MEDIUM_CANNON_L_DWN, strong, x, y + 1, playerParam, mediumCannon));
                    Game1.battlegroundMap.setMapCell(x + 1, y + 1, new MediumCannon_MapCell(TileType.MEDIUM_CANNON_R_DWN, strong, x + 1, y + 1, playerParam, mediumCannon));
                    
                    return true;
            }
            return false;
        
        }

        public override MapCell shootMe(Damage damage) {

            if (!StrongCell.damage(damage.Value)) {

                return base.change(TileType.NOT_CONQUERED);
            }

            return this;
        }

        /// <summary>
        /// Cuando cambia el estado conquistado del tile, es llamado este metodo.
        /// </summary>
        public override void changeConquered() {
            cannon.Enabled = conquered;
        }

    }
}